package net.web.dao;

import net.web.entity.BlogComment;
import net.web.common.utils.JdbcUtils; // 假设已有 JDBC 工具类，封装连接、执行逻辑
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BlogCommentDao {
    // 新增评论
    public boolean insert(BlogComment comment) {
        String sql = "INSERT INTO blog_comment (blogid, userid, content, username, authoremail, userpicture, createdat, updatedat) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
        try (Connection conn = JdbcUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setLong(1, comment.getBlogid());
            pstmt.setLong(2, comment.getUserid());
            pstmt.setString(3, comment.getContent());
            pstmt.setString(4, comment.getUsername());
            pstmt.setString(5, comment.getAuthoremail());
            pstmt.setString(6, comment.getUserpicture());
            pstmt.setTimestamp(7, comment.getCreatedat());
            pstmt.setTimestamp(8, comment.getUpdatedat());
            return pstmt.executeUpdate() > 0; // 执行插入，影响行数 >0 则成功
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 根据文章 ID 查询评论列表
    public List<BlogComment> getCommentsByBlogId(Long blogId) {
        List<BlogComment> comments = new ArrayList<>();
        String sql = "SELECT id, blogid, userid, content, username, authoremail, userpicture, createdat, updatedat " +
                "FROM blog_comment WHERE blogid = ? ORDER BY createdat ASC";
        try (Connection conn = JdbcUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setLong(1, blogId);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                BlogComment comment = new BlogComment();
                comment.setId((int) rs.getLong("id"));
                comment.setBlogid((int) rs.getLong("blogid"));
                comment.setUserid((int) rs.getLong("userid"));
                comment.setContent(rs.getString("content"));
                comment.setUsername(rs.getString("username"));
                comment.setAuthoremail(rs.getString("authoremail"));
                comment.setUserpicture(rs.getString("userpicture"));
                comment.setCreatedat(rs.getTimestamp("createdat"));
                comment.setUpdatedat(rs.getTimestamp("updatedat"));
                comments.add(comment);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return comments;
    }

    public boolean delete(int commentId) {
        String sql = "DELETE FROM blog_comment WHERE id = ?";
        try (Connection conn = JdbcUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, commentId);
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 其他方法：如根据 ID 删除评论、更新评论等，按需扩展
}
